package com.bb.blog.code.threading.example;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * ArrayBlockingQueue 不可扩容， 只能防止固定数量的元素
 */
public class ArrayBlockingQueueExample {
    private static ArrayBlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(1);

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();

        executorService.submit(()->{
            String name = Thread.currentThread().getName();
            for (int i = 0; i < 2; i++) {
                try {
                    blockingQueue.put("" +i);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(name + ": " + i);

            }
        });

        executorService.submit(()->{
            String name = Thread.currentThread().getName();
            int i = 0;
            while(true){
                Thread.sleep(20000);
                String take = blockingQueue.take();
                System.out.println(name + ": " + take);

            }
        });

    }
}
